package com.atguigu.gmall.product;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ssss {
    public static void main(String[] args) {
        //1.创建新线程
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                5, //核心线程
                10, //最大线程
                5, //闲置线程的存货时间
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                //工厂: 默认工厂
                //拒绝策略
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        CompletableFuture.supplyAsync(() -> {
            System.out.println("上一个任务" + Thread.currentThread().getName());
            return "123";
        }, threadPoolExecutor).whenCompleteAsync((t, u) -> {
            //计算完成时回调方法
            //1:处理返回值
            System.out.println("t:" + t);
            int x = 10/0;
            System.out.println("当前任务：" + Thread.currentThread().getName());
        }).exceptionally((u) -> { //处理异常
            System.out.println("u:" + u);
            System.out.println("下一个任务：" + Thread.currentThread().getName());
            return "处理成功";
        });

        System.out.println("抛出异常后的主线程"+Thread.currentThread().getName());
    }
}
